var config=require('../../config');
const detailsUrl = config.detailsUrl;
const fetaUrl = config.fetaUrl;
const commentUrl = config.commentUrl;
const gxUrl = config.gxUrl;
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: 'CHN', value: '货到付款（现金）', checked: 'true' },
      { name: 'USA', value: '货到付款（刷卡）' },
    ],
    row:0,
    show:true,
    title:'京顺堂大药房',
    stars: ['iphone6pule', 'iphone6s'],
    yanse: [],
    pinmu: ['4.7寸', '5.5寸'],
    pin:0 ,
    yan:1,
    num:1,
    price:0.00,//单价金额
    imageList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    wx.request({
      url: commentUrl+options.id,
      success:function(res){        
        that.setData({
          comment: res.data,
        })
      }
    });
    wx.request({
      url: detailsUrl+options.id, //仅为示例，并非真实的接口地址      
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {       
        wx.showNavigationBarLoading();
        wx.setNavigationBarTitle({
          title: res.data.title
        })
        wx.setStorage({
          key: "details", 
          data: res.data,         
        })

        var art=[];
        if (res.data.attribute != 'null') {
          art = res.data.attribute;
        }
        var dts = [];
        if (res.data.details != 'null') {
          dts = res.data.details;
        }
        var small_images = [];
        if (res.data.smallimg != 'null') {
          small_images = res.data.smallimg;
        }
        var imageList = that.data.imageList;
        imageList[0] = gxUrl + res.data.picture;
        for (var t in small_images){
          imageList[parseInt(t)+1] = gxUrl + small_images[t];
        }
        that.setData({
          details: res.data,
          art: art,
          dts: dts,
          small_images: small_images,
          catid: res.data.catid,
          title: res.data.title,
          yanse: res.data.sepcary,
          price: res.data.money,
          imageList: imageList
        })
        wx.hideNavigationBarLoading();
      }
    });
   
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {    
    var that=this; 
    return {
      title: that.data.title,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    var that = this;
    var rowt = that.data.row;
    var catid = that.data.catid;
   
    wx.request({
      url: fetaUrl + rowt + '/' + catid, //仅为示例，并非真实的接口地址      
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
      
        rowt = rowt + 1;
        var tdata = that.data.list;
        var dwx = res.data;
        if (tdata){
          dwx = tdata.concat(res.data);
        }        
        that.setData({
          list: dwx,
          row: rowt,
        })
      }
    });
  },
  /*
  * 立即购买 
  */
  buy:function(){
    var num=this.data.num;
    var pin = this.data.pin;
    var spec=this.data.yanse;
    var specnum=Object.getOwnPropertyNames(spec).length;
    if (pin > 0) {
      wx.navigateTo({
        url: '../myinfo/order/order_account/order_account?num=' + num+'&pin='+pin
      })
    }else{
      wx.showToast({
        title: '请选择规格参数',
        image:'/images/error.png',
      })
    }
  },
  buypay:function(){
    var num = this.data.num;
    var pin = this.data.pin;
    var spec = this.data.yanse;
    if(spec){
      var specnum = Object.getOwnPropertyNames(spec).length;
      if (specnum > 0) {
        this.setData({
          showView: true
        })
        return;
      }
    }    
    wx.navigateTo({
      url: '../myinfo/order/order_account/order_account'
    })
    
  },
  moveproduct:function(){
    wx.switchTab({
      url: '../new/new'
    })
  },
  //显示详情
  onsts:function(){
    this.setData({
      show:true
    })
  },
  //显示产品规格
  onsth: function () {
    this.setData({
      show: false
    })
  },

  /**
   * 显示与隐藏
   */
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  /**
   * 绑定事件
   */
  hpdd: function (e) {
    var id = e.currentTarget.dataset.id;
    var money = this.data.yanse[id].money;
    this.setData({
      price: money,
      pin: id,
    })
  },
  //加号
  jiahao:function(){
    var num=this.data.num;
    this.setData({     
      num: num+1
    })
  },
  //减号
  onjian: function () {
    var num = this.data.num;
    this.setData({    
      num: (num - 1) > 0 ? num-1:1,
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  }
  
})